﻿using DreamWork.Configuration;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace DreamWork.Core
{
    public static class MapperExtensions
    {
        public static T Map<T>(this ExpandoObject source)
        {
            T destination = Activator.CreateInstance<T>();
            IDictionary<string, object> dict = source;
            var type = destination.GetType();

            foreach (var prop in type.GetProperties())
            {
                var upper = prop.Name.ToUpper(CultureInfo.InvariantCulture);
                var key = dict.Keys.SingleOrDefault(k => k.ToUpper(CultureInfo.InvariantCulture) == upper);

                if (key != null)
                {
                    prop.SetValue(destination, dict[key], null);
                }
            }
            return destination;
        }
    }
}
